<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model33" :rules="validatorRules">


        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceBase" label="基地">
<!--          <a-input placeholder="请输入基地" v-model="model33.sourceBase" />-->
          <a-select v-model="model33.sourceBase"  placeholder='请选择所属基地'
                    @change='handleChange'>
            <a-select-option v-for='d in baseList' :key='d.id' :value='d.id'>{{ d.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sourceGreenhouse" label="地块">
<!--          <a-input placeholder="请输入地块" v-model="model33.sourceGreenhouse" />-->
          <a-select v-model="model33.sourceGreenhouse" placeholder='请输入地块'  >
            <a-select-option v-for='d in greenhouseList' :key='d.id' :value='d.id'>{{ d.name }}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="storehouse" label="库房">
<!--          <a-input placeholder="请输入库房" v-model="model33.storehouse" />-->
          <a-select v-model="model33.storehouse" placeholder='请选择冷库'>
            <a-select-option v-for='d in hoseList' :key='d.id' :value='d.code'>{{ d.name }}</a-select-option>
          </a-select>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productionId" label="批次">
<!--          <a-input placeholder="请输入批次" v-model="model33.productionId" />-->
          <j-dict-select-tag style='width: 100%'  placeholder="请选择批次" :dict-code="productions" v-model="model33.productionId"></j-dict-select-tag>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="variety" label="产品">
<!--          <a-input placeholder="请输入产品" v-model="model33.variety"  />-->
          <j-dict-select-tag  v-model="model33.variety"  :triggerChange='true' dictCode='t_flower_variety_zz,name,id'
                             placeholder='请输入产品'>
          </j-dict-select-tag>
        </a-form-model-item>

        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="flowerPoleLength" label="单位">
<!--          <a-input placeholder="请输入单位" v-model="model.flowerPoleLength" />-->
          <j-dict-select-tag type="list" v-model="model33.flowerPoleLength"  dictCode="	planting_flower_pole" placeholder="请选择单位"/>

        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="harvestCount" label="采收数量">
          <a-input-number v-model="model33.harvestCount"/>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="harvestTime" label="采摘日期">
          <a-date-picker  valueFormat='YYYY-MM-DD' v-model="model33.harvestTime" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import moment from "moment"
  import JDate from '@/components/jeecg/JDate'
  import { queryFlowerBase, ajaxGetDictItems, queryGreenhouseByPd , getVarietyNameList ,listByPsn} from '@/api/vegetableAPI'
  import { getTodayDate,getNowTime } from '../../../utils/getDate'

  export default {
    name: "FlowerHarvestInfoModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model33: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        baseList: [], //基地信息
        greenhouseList: [], //大棚信息
        hoseList: [], //冷库
        confirmLoading: false,
        productions: "t_flower_production,code,code",
        validatorRules:{
          sourceBase:[{ required: true, message: '请输入基地!' }],
          sourceGreenhouse:[{ required: true, message: '请输入地块!' }],
          storehouse:[{ required: true, message: '请输入库房!' }],
          variety:[{ required: true, message: '请输入产品!' }],
          productionId:[{ required: true, message: '请输入批次!' }],
          flowerPoleLength:[{ required: true, message: '请输入单位!' }],
          harvestCount:[{ required: true, message: '请输入采收数量!' }],
          harvestTime:[{ required: true, message: '请输入采收时间!' }],
        },
        url: {
          add: "/chvegetable/flowerHarvestInfo/add",
          edit: "/chvegetable/flowerHarvestInfo/edit",
        },
      }
    },
    created () {
      this.initBaseInfo()
      this.initVarietyNameList()
      this.initStorehoseList()
    },
    methods: {
      add () {
        //初始化默认值
        this.edit({});

        this.model33.harvestTime = getTodayDate()
      },
      edit (record) {
        this.model33 = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
         this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model33.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model33,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false;
          }
        })
      },
      handleCancel () {
        this.close()
      },

      handleChange(value) {

        queryGreenhouseByPd(value).then((res) => {
          if (res.success) {
            this.greenhouseList = res.result

          }
        })

        // this.model33.sourceGreenhouse = undefined
        delete this.model33.sourceGreenhouse
        this.$refs['form'].validateField('sourceGreenhouse');

        this.productions = 't_flower_production,code,code,mainid=\'' + value + '\''
        delete this.model33.productionId


        // this.$refs['form']['model'].setFieldsValue({"sourceGreenhouse":''})
        // this.$refs['form']['model']['sourceGreenhouse']=undefined
        // this.$refs['form'].$attrs("sourceGreenhouse","")
      },
      handleEditChange(value) {
        // console.log(value)
        queryGreenhouseByPd(value).then((res) => {
          if (res.success) {
            this.greenhouseList = res.result
          }
        })
      },
      //初始化基地信息
      initBaseInfo() {
        queryFlowerBase().then((res) => {
          if (res.success) {
            this.baseList = res.result
          }
        })
      },  //冷库
      initStorehoseList() {
        listByPsn().then((res) => {
          if (res.success) {
            this.hoseList = res.result
          }
        })
      },
      initClassifyDict() {
        const dictCode = 'planting_classify'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.classifyList = res.result
          }
        })
      },
      initVarietyDict() {
        const dictCode = 'planting_variety'
        ajaxGetDictItems(dictCode, null).then((res) => {
          if (res.success) {
            this.varietyList = res.result
          }
        })
      },
    initVarietyNameList() {
      const that = this
      getVarietyNameList().then((res)=>{

        if(res.success){
          // that.varietyNameList = res.result
          this.varietyNameList = res.result.map((item,index,arr)=>{
            let c = {label:item.name, value:item.name}
            return c;
          })
        }else{
          that.$message.warning(res.message)
        }
      });
    }
    }
  }
</script>

<style lang="less" scoped>

</style>